package com.neuedu.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neuedu.dao.impl.FangChanDaoImpl;
import com.neuedu.model.DongShu;
import com.neuedu.model.FangChan;
import com.neuedu.model.XiaoQu;
import com.neuedu.service.FangChanService;
import com.neuedu.service.impl.FangChanServiceImpl;
import com.neuedu.util.R;
import com.neuedu.vo.PageVO;

import java.util.List;

public class FangChanController {
    private FangChanService fangChanService=new FangChanServiceImpl();

    public R page(JSONObject jsonObject){
        Integer pageNo=jsonObject.getInteger("pageNo");
        Integer pageSize=jsonObject.getInteger("pageSize");
        //在某两个日期之间，like 一个给定的小区 名称。
        String startTime=jsonObject.getString("startTime");
        String endTime=jsonObject.getString("endTime");
        String name=jsonObject.getString("name");

        PageVO<FangChan> pageVO = fangChanService.selectOnePage(pageNo, pageSize, startTime, endTime, name);
        return R.ok().setData(pageVO);
    }

    public R add(JSONObject jsonObject){
        String suoshuxiaoqu = jsonObject.getString("suoshuxiaoqu");
        String dongshumingcheng = jsonObject.getString("dongshumingcheng");
        String fangchanmingcheng = jsonObject.getString("fangchanmingcheng");
        String huzhumingcheng = jsonObject.getString("huzhumingcheng");
        String lianxifangshi = jsonObject.getString("lianxifangshi");
        String fangjianshu = jsonObject.getString("fangjianshu");
        String danyuan = jsonObject.getString("danyuan");
        String louceng = jsonObject.getString("louceng");
        String miaoshu = jsonObject.getString("miaoshu");

        FangChan fangChan=new FangChan();
        fangChan.setSuoshuxiaoqu(suoshuxiaoqu);
        fangChan.setDongshu(dongshumingcheng);
        fangChan.setFangchanmingcheng(fangchanmingcheng);
        fangChan.setHuzhumingcheng(huzhumingcheng);
        fangChan.setLianxifangshi(lianxifangshi);
        fangChan.setFangjianshu(fangjianshu);
        fangChan.setDanyuan(danyuan);
        fangChan.setLouceng(louceng);
        fangChan.setMiaoshu(miaoshu);

        //调用service，录入一个新的栋数
        int result = fangChanService.add(fangChan);
        if(result>0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }

    }


    /**
     * 修改展示信息
     * @param jsonObject
     * @return
     */
    public R showFangChan(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        FangChan fangChan = fangChanService.findFangChanById(id);
        return R.ok().setData(fangChan);
    }


    public R edit(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        FangChan fangChan = fangChanService.findFangChanById(id);
        String suoshuxiaoqu = jsonObject.getString("suoshuxiaoqu");
        String dongshumingcheng = jsonObject.getString("dongshumingcheng");
        String fangchanmingcheng = jsonObject.getString("fangchanmingcheng");
        String huzhumingcheng = jsonObject.getString("huzhumingcheng");
        String lianxifangshi = jsonObject.getString("lianxifangshi");
        String fangjianshu = jsonObject.getString("fangjianshu");
        String danyuan = jsonObject.getString("danyuan");
        String louceng = jsonObject.getString("louceng");
        String miaoshu = jsonObject.getString("miaoshu");

        fangChan.setSuoshuxiaoqu(suoshuxiaoqu);
        fangChan.setDongshu(dongshumingcheng);
        fangChan.setFangchanmingcheng(fangchanmingcheng);
        fangChan.setHuzhumingcheng(huzhumingcheng);
        fangChan.setLianxifangshi(lianxifangshi);
        fangChan.setFangjianshu(fangjianshu);
        fangChan.setDanyuan(danyuan);
        fangChan.setLouceng(louceng);
        fangChan.setMiaoshu(miaoshu);

        //调用service，录入一个新的栋数
        int result = fangChanService.editFangChanInfo(fangChan);
        if(result>0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }

    }

    public R del(JSONObject jsonObject){
        JSONArray delId = jsonObject.getJSONArray("delId");
        Integer [] deleteId=new Integer[delId.size()];
        for (int i = 0; i < delId.size(); i++) {
            deleteId[i]=delId.getInteger(i);
        }

        int result = fangChanService.del(deleteId);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }

    }


    public R findFangChanByXiaoQuName(JSONObject jsonObject){
        String xiaoQuName = jsonObject.getString("communityName");
        List<FangChan> fangChans = fangChanService.findFangChanByXiaoquName(xiaoQuName);

        return R.ok().setData(fangChans);
    }


}
